package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
/*
* 添加分类
* */
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("要添加的分类是：{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();

    }
    @GetMapping("/page")
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询分类：{}",categoryPageQueryDTO);
        PageResult pageResult=categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    @DeleteMapping
    public Result delete(Long id){
        log.info("删除分类：{}",id);
        categoryService.delete(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getId(@PathVariable Long id){
        log.info("查询分类信息：{}"+id);
        Category category =categoryService.getId(id);
        return Result.success(category);
    }
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("id:"+id+"分类管理的，状态修改为:{}"+status);
        categoryService.startOrStop(status,id);
        return Result.success();
    }
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
       log.info("修改分类：{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }
    //根据类型type查询分类
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        log.info("根据分类类型：{}",type);
        List<Category> categoryList=categoryService.list(type);
        return Result.success(categoryList);
    }
}
